package com.mofang.activity.interact;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.mofang.R;
import com.mofang.activity.BaseTitleViewActiviey;
import com.mofang.adapters.YearLengthSelectAdapter;
import com.mofang.adapters.YearLengthSelectAdapter.LoanTime;
import com.mofang.utils.InputChecker;
import com.mofang.utils.LoanCalculate;
import com.pactera.framework.util.ToastHelper;

public class LoanCalculateActivity extends BaseTitleViewActiviey implements
		OnClickListener {

	private LinearLayout gjjLoanBtn_ly;// 公积金贷款
	private LinearLayout syLoanBtn_ly;// 商业贷款
	private LinearLayout groupLoanBtn_ly;// 组合贷款
	private LinearLayout gjjLoanSum_ly;// 公积金贷款金额
	private LinearLayout syLoanSum_ly;// 商业贷款金额
	private LinearLayout gjjLoanProfit_ly;// 公积金贷款利润
	private LinearLayout syjLoanProfit_ly;// 商业贷款利润

	private EditText loan_calculator_gjjLoanSum_edit; // 公积金贷款总金额
	private EditText loan_calculator_syLoanSum_edit; // 商业贷款总金额

	private EditText edit_gjjLoanProfit; // 公积金贷款利率，输入部分
	
	private LinearLayout ll_syjLoanProfit; // 商业贷款利率，选择部分
	private TextView tv_loan_calculator_syjLoanProfit;
	private EditText edit_syjLoanProfit; // 商业贷款利率，输入部分
	
	private LinearLayout ll_loan_calculator_year_select;// 贷款年数
	private TextView tv_loan_calculator_year; // 贷款年数
	private int calculator_year; // 贷款年数
	private RadioButton rb_debx; // 等额本息
	private RadioButton rb_debj; // 等额本金

	private LinearLayout startCal_ly;// 开始计算

	private TextView gjjLoanSum_text;
	private TextView syLoanSum_text;
	private TextView gjjLoanProfit_text;
	private TextView syjLoanProfit_text;
	private TextView tap1;
	private TextView tap2;
	private TextView tap3;
	private View loan_calculator_line;

	private int lendingModel;// 当前贷款模式

	private PopupWindow mPopupWindow_year;
	private PopupWindow mPopupWindow_syjLoanProfit;
	
	ListView listViewTime;
	ListView listViewProfit;
	private ArrayList<LoanTime> data;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loan_calculate);
		setNavigationTitle("贷款计算器");
		setNavigationLeftBtnImage(R.drawable.return_arrow);
		findView();
		
	}
	private void setCurrentYear(int position) {
		//TODO
		listViewTime.setSelection(position);
		listViewProfit.setSelection(position);
		tv_loan_calculator_year.setText(data.get(position).getLongtime());
		tv_loan_calculator_syjLoanProfit.setText(data.get(position).getLongtime());
		edit_syjLoanProfit.setText(data.get(position).getProfit()+"");
		calculator_year=data.get(position).getYear();
	}

	private void findView() {
		listViewTime=new ListView(mContext);
		listViewProfit=new ListView(mContext);
		data=new ArrayList<YearLengthSelectAdapter.LoanTime>();
		for (int i = 1; i <= 30; i++) {
			data.add(new LoanTime(i));
		}
		YearLengthSelectAdapter adapter=new YearLengthSelectAdapter(mContext, data);
		listViewTime.setAdapter(adapter);
		listViewProfit.setAdapter(adapter);
		listViewTime.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				setCurrentYear(position);
				mPopupWindow_year.dismiss();
			}

		});
		listViewProfit.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				setCurrentYear(position);

				mPopupWindow_syjLoanProfit.dismiss();
			}
		});
		
		ll_syjLoanProfit = (LinearLayout) findViewById(R.id.ll_syjLoanProfit);
		ll_syjLoanProfit.setOnClickListener(this);
		tv_loan_calculator_syjLoanProfit=(TextView) findViewById(R.id.tv_loan_calculator_syjLoanProfit);
		
		tv_loan_calculator_year = (TextView) findViewById(R.id.tv_loan_calculator_year);
		edit_syjLoanProfit = (EditText) findViewById(R.id.et_loan_calculator_syjLoanProfit);
		edit_gjjLoanProfit = (EditText) findViewById(R.id.et_loan_calculator_gjjLoanProfit);
//XXX
		ll_loan_calculator_year_select=(LinearLayout) findViewById(R.id.ll_loan_calculator_year_select);
		ll_loan_calculator_year_select.setOnClickListener(this);

		rb_debx = (RadioButton) findViewById(R.id.rb_debx);
		rb_debj = (RadioButton) findViewById(R.id.rb_debj);

		loan_calculator_gjjLoanSum_edit = (EditText) findViewById(R.id.loan_calculator_gjjLoanSum_edit);
		loan_calculator_syLoanSum_edit = (EditText) findViewById(R.id.loan_calculator_syLoanSum_edit);
		
		gjjLoanBtn_ly = (LinearLayout) findViewById(R.id.loan_calculator_gjjLoanBtn_ly);
		gjjLoanBtn_ly.setOnClickListener(this);

		syLoanBtn_ly = (LinearLayout) findViewById(R.id.loan_calculator_syLoanBtn_ly);
		syLoanBtn_ly.setOnClickListener(this);

		groupLoanBtn_ly = (LinearLayout) findViewById(R.id.loan_calculator_groupLoanBtn_ly);
		groupLoanBtn_ly.setOnClickListener(this);

		gjjLoanSum_ly = (LinearLayout) findViewById(R.id.loan_calculator_gjjLoanSum_ly);
		gjjLoanSum_ly.setOnClickListener(this);

		syLoanSum_ly = (LinearLayout) findViewById(R.id.loan_calculator_syLoanSum_ly);
		syLoanSum_ly.setOnClickListener(this);

		gjjLoanProfit_ly = (LinearLayout) findViewById(R.id.loan_calculator_gjjLoanProfit_ly);
		gjjLoanProfit_ly.setOnClickListener(this);

		syjLoanProfit_ly = (LinearLayout) findViewById(R.id.loan_calculator_syjLoanProfit_ly);
		syjLoanProfit_ly.setOnClickListener(this);

		startCal_ly = (LinearLayout) findViewById(R.id.loan_calculator_startCal_ly);
		startCal_ly.setOnClickListener(this);

		gjjLoanSum_text = (TextView) findViewById(R.id.loan_calculator_gjjLoanSum_text);
		syLoanSum_text = (TextView) findViewById(R.id.loan_calculator_syLoanSum_text);
		gjjLoanProfit_text = (TextView) findViewById(R.id.loan_calculator_gjjLoanProfit_text);
		syjLoanProfit_text = (TextView) findViewById(R.id.loan_calculator_syjLoanProfit_text);
		loan_calculator_line = (View) findViewById(R.id.loan_calculator_line);

		tap1 = (TextView) findViewById(R.id.text_gjj);
		tap2 = (TextView) findViewById(R.id.text_sy);
		tap3 = (TextView) findViewById(R.id.text_group);

		tap1.setTextColor(getResources().getColor(R.color.black_green));
		lendingModel = LoanCalculate.Gjj_loan;
		gjjLoanBtn_ly.setBackgroundResource(R.drawable.gjjloanbtnbg_select);
		syLoanBtn_ly.setBackgroundResource(R.drawable.syloanbtnbg);
		groupLoanBtn_ly.setBackgroundResource(R.drawable.grouploanbtnbg);

	}

	@Override
	public void onClick(View v) {

		Intent intent2;
		switch (v.getId()) {
		case R.id.ll_loan_calculator_year_select:
			if (mPopupWindow_year == null) {
				int mPopupWindowWidth= ll_loan_calculator_year_select.getWidth();
				mPopupWindow_year = new PopupWindow(listViewTime,mPopupWindowWidth,
						LayoutParams.WRAP_CONTENT);
				mPopupWindow_year.setFocusable(true); // 设置PopupWindow可获得焦点
				mPopupWindow_year.setTouchable(true); // 设置PopupWindow可触摸
				mPopupWindow_year.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
				mPopupWindow_year.setAnimationStyle(R.style.PopwindowAnimation);
				mPopupWindow_year.setBackgroundDrawable(new PaintDrawable());
			}
			mPopupWindow_year.showAsDropDown(ll_loan_calculator_year_select, 0, 0);
			break;
		case R.id.ll_syjLoanProfit:
			if (mPopupWindow_syjLoanProfit == null) {
				int mPopupWindowWidth= ll_syjLoanProfit.getWidth();
				mPopupWindow_syjLoanProfit = new PopupWindow(listViewProfit,mPopupWindowWidth,
						LayoutParams.WRAP_CONTENT);
				mPopupWindow_syjLoanProfit.setFocusable(true); // 设置PopupWindow可获得焦点
				mPopupWindow_syjLoanProfit.setTouchable(true); // 设置PopupWindow可触摸
				mPopupWindow_syjLoanProfit.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
				mPopupWindow_syjLoanProfit.setAnimationStyle(R.style.PopwindowAnimation);
				mPopupWindow_syjLoanProfit.setBackgroundDrawable(new PaintDrawable());
			}
			mPopupWindow_syjLoanProfit.showAsDropDown(ll_syjLoanProfit, 0, 0);
			break;
		case R.id.loan_calculator_gjjLoanBtn_ly:
			lendingModel = LoanCalculate.Gjj_loan;
			gjjLoanSum_ly.setVisibility(View.VISIBLE);
			gjjLoanProfit_ly.setVisibility(View.VISIBLE);
			syLoanSum_ly.setVisibility(View.GONE);
			syjLoanProfit_ly.setVisibility(View.GONE);
			loan_calculator_line.setVisibility(View.GONE);
			break;

		case R.id.loan_calculator_syLoanBtn_ly:
			lendingModel = LoanCalculate.Sy_loan;
			gjjLoanSum_ly.setVisibility(View.GONE);
			syLoanSum_ly.setVisibility(View.VISIBLE);
			gjjLoanProfit_ly.setVisibility(View.GONE);
			syjLoanProfit_ly.setVisibility(View.VISIBLE);
			loan_calculator_line.setVisibility(View.GONE);

			break;

		case R.id.loan_calculator_groupLoanBtn_ly:
			lendingModel = LoanCalculate.Group_loan;
			gjjLoanSum_ly.setVisibility(View.VISIBLE);
			syLoanSum_ly.setVisibility(View.VISIBLE);
			gjjLoanProfit_ly.setVisibility(View.VISIBLE);
			syjLoanProfit_ly.setVisibility(View.VISIBLE);
			loan_calculator_line.setVisibility(View.VISIBLE);
			break;

		case R.id.loan_calculator_startCal_ly:
			// TODO 计算
			LoanCalculate calculate = new LoanCalculate();
			if (!inputChecked()) {
				return;
			}
			intent2 = new Intent(getApplicationContext(),
					LoanCalculateResultActivity.class);
			calculate.setLendingModel(lendingModel);
			if (rb_debj.isChecked()) {
				calculate.setRepayment(LoanCalculate.Repay_debj);
			} else {
				calculate.setRepayment(LoanCalculate.Repay_debx);
			}
			if (!InputChecker.isEmpty(loan_calculator_gjjLoanSum_edit
					.getText().toString())) {
				int lc_gjjtotal = Integer
						.valueOf(loan_calculator_gjjLoanSum_edit.getText()
								.toString());
				calculate.setLc_gjjtotal(lc_gjjtotal);
			}
			if (!InputChecker.isEmpty(edit_syjLoanProfit
					.getText().toString())) {
				float lc_syjProfit = Float
						.valueOf(edit_syjLoanProfit.getText()
								.toString());
				calculate.setLc_syjProfit(lc_syjProfit);
			}
			if (!InputChecker.isEmpty(loan_calculator_syLoanSum_edit.getText()
					.toString())) {
				int lc_syjtotal = Integer
						.valueOf(loan_calculator_syLoanSum_edit.getText()
								.toString());
				calculate.setLc_syjtotal(lc_syjtotal);
			}
			if (!InputChecker.isEmpty(edit_gjjLoanProfit
					.getText().toString())) {
				float lc_gjjProfit = Float
						.valueOf(edit_gjjLoanProfit.getText()
								.toString());
				calculate.setLc_gjjProfit(lc_gjjProfit);
			}
				calculate.setLc_yearLongth(calculator_year);
			intent2.putExtra("calculate", calculate);
			startActivity(intent2);
			break;

		default:
			break;
		}

		switch (lendingModel) {
		case LoanCalculate.Gjj_loan:
			tap1.setTextColor(getResources().getColor(R.color.black_green));
			tap2.setTextColor(getResources().getColor(R.color.textColor3));
			tap3.setTextColor(getResources().getColor(R.color.textColor3));
			gjjLoanBtn_ly.setBackgroundResource(R.drawable.gjjloanbtnbg_select);
			syLoanBtn_ly.setBackgroundResource(R.drawable.syloanbtnbg);
			groupLoanBtn_ly.setBackgroundResource(R.drawable.grouploanbtnbg);
			break;
		case LoanCalculate.Sy_loan:
			tap1.setTextColor(getResources().getColor(R.color.textColor3));
			tap2.setTextColor(getResources().getColor(R.color.black_green));
			tap3.setTextColor(getResources().getColor(R.color.textColor3));
			gjjLoanBtn_ly.setBackgroundResource(R.drawable.gjjloanbtnbg);
			syLoanBtn_ly.setBackgroundResource(R.drawable.gjjloanbtnbg_select);
			groupLoanBtn_ly.setBackgroundResource(R.drawable.grouploanbtnbg);
			break;
		case LoanCalculate.Group_loan:
			tap1.setTextColor(getResources().getColor(R.color.textColor3));
			tap2.setTextColor(getResources().getColor(R.color.textColor3));
			tap3.setTextColor(getResources().getColor(R.color.black_green));
			gjjLoanBtn_ly.setBackgroundResource(R.drawable.gjjloanbtnbg);
			syLoanBtn_ly.setBackgroundResource(R.drawable.syloanbtnbg);
			groupLoanBtn_ly
					.setBackgroundResource(R.drawable.gjjloanbtnbg_select);
			break;

		default:
			break;
		}
	}

	private boolean inputChecked() {
		if (calculator_year==0) {
			ToastHelper.getInstance().showLongMsg("先填写按揭年数");
			return false;
		}
		if (!rb_debj.isChecked() && !rb_debx.isChecked()) {
			ToastHelper.getInstance().showLongMsg("先选择还款方式");
			return false;
		}
		return true;
	}

}
